package com.hsq.androidproject.ui;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyListViewStarRankingAdapter;
import com.hsq.androidproject.db.DBHelper;
import com.hsq.androidproject.entity.HotStars;
import com.hsq.androidproject.entity.bd_entity.Follow;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;

public class ShowStarRankingActivity extends FragmentActivity {
    private String path = "http://api.hanju.koudaibaobao.com/api/star/hotStars?offset=0&count=30";

    private ImageView iv_count1, iv_count2, iv_count3;    /////前三名头像
    private TextView tv_name1, tv_name2, tv_name3;        /////前三名姓名
    private TextView tv_follow1, tv_follow2, tv_follow3; /////前三名关注

    private MyListViewStarRankingAdapter adapter;
    private List<HotStars.HotStarsBean> hotStarsBeen;
    private ListView mListView;

    private DBHelper dbHelper;          ///////数据库 是否关注
    String usersName = "peng";           ////用来判断是否登录
    private HotStars.HotStarsBean hotStars1;
    private HotStars.HotStarsBean hotStars2;
    private HotStars.HotStarsBean hotStars3;
    private Follow follow1;
    private Follow follow2;
    private Follow follow3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_star_ranking);

        initView();

         /*前三名*/
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {

            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(ShowStarRankingActivity.this, "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                HotStars hotStars = new Gson().fromJson(response, HotStars.class);
                hotStarsBeen = hotStars.getHotStars();

                //////第一名
                hotStars1 = hotStarsBeen.get(0);
                Glide.with(ShowStarRankingActivity.this)  //头像
                        .load(hotStars1.getThumb())
                        .transform(new GlideCircleTransform(ShowStarRankingActivity.this))//圆形
                        //  .transform(new GlideRoundTransform(ShowStarRankingActivity.this, 10))//圆角
                        .into(iv_count1);
                tv_name1.setText(hotStars1.getName());  //姓名


                //////第二名
                hotStars2 = hotStarsBeen.get(1);
                Glide.with(ShowStarRankingActivity.this)
                        .load(hotStars2.getThumb())
                        .transform(new GlideCircleTransform(ShowStarRankingActivity.this))//圆形
                        .into(iv_count2);
                tv_name2.setText(hotStars2.getName());

                //////第三名
                hotStars3 = hotStarsBeen.get(2);
                Glide.with(ShowStarRankingActivity.this)
                        .load(hotStars3.getThumb())
                        .transform(new GlideCircleTransform(ShowStarRankingActivity.this))//圆形
                        .into(iv_count3);
                tv_name3.setText(hotStars3.getName());

                follow1 = new Follow(tv_name1.getText().toString(), usersName);
                if (dbHelper.isExistF(follow1)) {             //////已经关注
                    tv_follow1.setText("已关注");
                } else {                                        ///////没有关注
                    tv_follow1.setText("+关注");
                }
                follow2 = new Follow(tv_name2.getText().toString(), usersName);
                if (dbHelper.isExistF(follow2)) {             //////已经关注
                    // Toast.makeText(ShowStarRankingActivity.this, "已关注" + tv_name2.getText().toString(), Toast.LENGTH_SHORT).show();
                    tv_follow2.setText("已关注");
                } else {
                    // Toast.makeText(ShowStarRankingActivity.this, "+关注" + tv_name2.getText().toString(), Toast.LENGTH_SHORT).show();///////没有关注
                    tv_follow2.setText("+关注");
                }
                follow3 = new Follow(tv_name3.getText().toString(), usersName);
                if (dbHelper.isExistF(follow3)) {             //////已经关注
                    tv_follow3.setText("已关注");
                } else {                                        ///////没有关注
                    tv_follow3.setText("+关注");
                }

                List<HotStars.HotStarsBean> starsBeanList = new ArrayList<HotStars.HotStarsBean>();     /////listView
                for (int i = 3; i < hotStarsBeen.size(); i++) {
                    starsBeanList.add(hotStarsBeen.get(i));
                }
                adapter = new MyListViewStarRankingAdapter(starsBeanList, ShowStarRankingActivity.this);
                mListView.setAdapter(adapter);
                setListViewHeightBasedOnChildren(mListView);
            }
        });

        tv_follow1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dbHelper.isExistF(follow1)) {                //////已经关注
                    dbHelper.deleteFollowByNameAndUserName(follow1);
                    tv_follow1.setText("+关注");
                } else {                                       ///////没有关注
                    Follow collect = new Follow(hotStars1.getName(), usersName, hotStars1.getBaikeUrl(), hotStars1.getThumb(), hotStars1.getSid(), hotStars1.getRank(), hotStars1.getFansCount());
                    dbHelper.insert(collect);
                    Toast.makeText(ShowStarRankingActivity.this, "" + collect.toString(), Toast.LENGTH_SHORT).show();
                    tv_follow1.setText("已关注");
                }
            }
        });
        tv_follow2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dbHelper.isExistF(follow2)) {             //////已经关注
                    dbHelper.deleteFollowByNameAndUserName(follow2);
                    tv_follow2.setText("+关注");
                } else {                                        ///////没有关注
                    Follow collect = new Follow(hotStars2.getName(), usersName, hotStars2.getBaikeUrl(), hotStars2.getThumb(), hotStars2.getSid(), hotStars2.getRank(), hotStars2.getFansCount());
                    dbHelper.insert(collect);
                    tv_follow2.setText("已关注");
                }
            }
        });
        tv_follow3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dbHelper.isExistF(follow3)) {             //////已经关注
                    dbHelper.deleteFollowByNameAndUserName(follow3);
                    tv_follow3.setText("+关注");
                } else {                                        ///////没有关注
                    Follow collect = new Follow(hotStars3.getName(), usersName, hotStars3.getBaikeUrl(), hotStars3.getThumb(), hotStars3.getSid(), hotStars3.getRank(), hotStars3.getFansCount());
                    dbHelper.insert(collect);
                    tv_follow3.setText("已关注");
                }
            }
        });
    }


    private void initView() {
        dbHelper = new DBHelper(this);
        iv_count1 = (ImageView) findViewById(R.id.iv_count1);
        iv_count2 = (ImageView) findViewById(R.id.iv_count2);
        iv_count3 = (ImageView) findViewById(R.id.iv_count3);
        tv_name1 = (TextView) findViewById(R.id.tv_name1);
        tv_name2 = (TextView) findViewById(R.id.tv_name2);
        tv_name3 = (TextView) findViewById(R.id.tv_name3);
        tv_follow1 = (TextView) findViewById(R.id.tv_follow1);
        tv_follow2 = (TextView) findViewById(R.id.tv_follow2);
        tv_follow3 = (TextView) findViewById(R.id.tv_follow3);

        mListView = (ListView) findViewById(R.id.mListView);
    }

    public void click_search(View view) {
        startActivity(new Intent(this, SearchActivity.class));
    }

    public void click_back(View view) {
        finish();
    }

    public void click_rl1(View view) {
        String name = hotStars1.getName();
        String baileUrl = hotStars1.getBaikeUrl();
        String thumb = hotStars1.getThumb();
        int sid = hotStars1.getSid();
        int rank = hotStars1.getRank();
        int fansCount = hotStars1.getFansCount();
        Intent intent = new Intent(ShowStarRankingActivity.this, ShowHotStartsActivity.class);        ////展示每个明星的信息
        intent.putExtra("name", name);
        intent.putExtra("baileUrl", baileUrl);
        intent.putExtra("thumb", thumb);
        intent.putExtra("sid", sid);
        intent.putExtra("rank", rank);
        intent.putExtra("fansCount", fansCount);
        startActivity(intent);
    }

    public void click_rl2(View view) {
        String name = hotStars2.getName();
        String baileUrl = hotStars2.getBaikeUrl();
        String thumb = hotStars2.getThumb();
        int sid = hotStars2.getSid();
        int rank = hotStars2.getRank();
        int fansCount = hotStars2.getFansCount();
        Intent intent = new Intent(ShowStarRankingActivity.this, ShowHotStartsActivity.class);        ////展示每个明星的信息
        intent.putExtra("name", name);
        intent.putExtra("baileUrl", baileUrl);
        intent.putExtra("thumb", thumb);
        intent.putExtra("sid", sid);
        intent.putExtra("rank", rank);
        intent.putExtra("fansCount", fansCount);
        startActivity(intent);
    }

    public void click_rl3(View view) {
        String name = hotStars3.getName();
        String baileUrl = hotStars3.getBaikeUrl();
        String thumb = hotStars3.getThumb();
        int sid = hotStars3.getSid();
        int rank = hotStars3.getRank();
        int fansCount = hotStars3.getFansCount();
        Intent intent = new Intent(ShowStarRankingActivity.this, ShowHotStartsActivity.class);        ////展示每个明星的信息
        intent.putExtra("name", name);
        intent.putExtra("baileUrl", baileUrl);
        intent.putExtra("thumb", thumb);
        intent.putExtra("sid", sid);
        intent.putExtra("rank", rank);
        intent.putExtra("fansCount", fansCount);
        startActivity(intent);
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        MyListViewStarRankingAdapter listAdapter = (MyListViewStarRankingAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
